#include <fcntl.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#define FRAME_SIZE 512
#define FRAME_HEADER_SIZE sizeof(FrameHeader)
#define FRAME_TAILER_SIZE sizeof(FrameTailer)
#define RET_FAILED -1
#define RET_SUCCESS 0

typedef struct {
    uint16_t head;
    uint8_t  type_mode;
    uint8_t  sequence_id;
    uint16_t sender_id;
} FrameHeader;

typedef struct {
    uint16_t crc;
    uint16_t trailer;
} FrameTailer;

int serial_fd;

uint16_t crc16(const uint8_t *data, size_t len)
{
    uint16_t crc = 0xFFFF;
    for (size_t i = 0; i < len; i++) {
        crc ^= data[i];
        for (size_t j = 0; j < 8; j++) {
            if (crc & 0x0001) {
                crc = (crc >> 1) ^ 0xA001;
            } else {
                crc = (crc >> 1);
            }
        }
    }
    // printf("crc = %04X\n", crc);
    return crc;
}

// 发送一个帧
void send_frame(uint8_t *frame, size_t size)
{
    for (size_t i = 0; i < size; i++) {
        if (0 == i % 30) {
            putchar('\n');
        }
        printf("%02X ", frame[i]);
    }
    printf("\n");
}

size_t pack_frame(uint8_t *data, size_t data_size, uint8_t *frame, uint8_t frame_id)
{
    FrameHeader header = {
        .head        = 0x5555,
        .type_mode   = 1,
        .sequence_id = frame_id,
        .sender_id   = 0x0001};
    FrameTailer tailer = {
        .crc     = crc16(data, data_size),
        .trailer = 0xaaaa};
    size_t frame_size = FRAME_HEADER_SIZE + data_size + FRAME_TAILER_SIZE;
    memcpy(frame, &header, FRAME_HEADER_SIZE);
    memcpy(frame + FRAME_HEADER_SIZE, data, data_size);
    memcpy(frame + FRAME_HEADER_SIZE + data_size, &tailer, FRAME_TAILER_SIZE);
    return frame_size;
}

int init_serial_port(char *portname, int baudrate)
{
    int            fd;
    struct termios tty;

    // 打开串口设备文件
    if ((fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC)) < RET_SUCCESS) {
        perror("open");
        return RET_FAILED;
    }

    // 获取并修改串口参数
    memset(&tty, 0, sizeof(tty));
    if (tcgetattr(fd, &tty) != RET_SUCCESS) {
        perror("tcgetattr");
        return RET_FAILED;
    }

    cfsetospeed(&tty, baudrate);
    cfsetispeed(&tty, baudrate);
    // 这段代码配置了串口的以下信息：
    tty.c_cflag |= (CLOCAL | CREAD); // CLOCAL 和 CREAD 标志位被设置，使得串口被配置为本地连接和可读取状态。
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;      // CSIZE 标志位被清零，然后 CS8 标志位被设置，将数据位设置为 8 位。
    tty.c_cflag &= ~PARENB;  // PARENB 标志位被清零，禁用奇偶校验。
    tty.c_cflag &= ~CSTOPB;  // CSTOPB 标志位被清零，设置停止位为 1 位。
    tty.c_cflag &= ~CRTSCTS; // CRTSCTS 标志位被清零，禁用硬件流控制。

    tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // ICANON、ECHO、ECHOE 和 ISIG 标志位被清零，关闭规范模式、回显和信号处理。
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);         // IXON、IXOFF 和 IXANY 标志位被清零，禁用软件流控制。
    tty.c_iflag &= ~(ICRNL | INLCR);                // ICRNL 和 INLCR 标志位被清零，禁用回车换行处理。
    tty.c_oflag &= ~OPOST;                          // OPOST 标志位被清零，禁用输出处理。

    tty.c_cc[VTIME] = 10;
    tty.c_cc[VMIN]  = 0;

    if (tcsetattr(fd, TCSANOW, &tty) != RET_SUCCESS) {
        perror("tcsetattr");
        return RET_FAILED;
    }

    return fd;
}
int read_from_serial_port(int fd)
{
    char buf[1024];
    int  len = read(fd, buf, 1024 - 1);
    if (len < 0) {
        perror("read");
        return RET_FAILED;
    }
    buf[len] = '\0';
    printf("R:%s", buf);
    fflush(stdout);
    return len;
}
void *read_serial(void *arg)
{

    char buf[1024];
    int  res;
    while (1) {
        res = read(serial_fd, buf, 1024);
        if (res > 0) {
            buf[res] = 0;
            printf("%s", buf);
            fflush(stdout);
        }
    }
}
int main()
{
    uint8_t data[4096] = {
        254, 248, 228, 194, 152, 114, 88, 78, 80, 91, 103, 114, 122, 125, 126, 125,
        123, 121, 119, 119, 119, 119, 119, 119, 120, 120, 120, 120, 120, 120, 120,
        119, 119, 119, 119, 119, 118, 118, 116, 113, 107, 96, 82, 64, 45, 27, 13, 4,
        0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 3, 7, 16, 30, 50, 74, 99, 123,
        146, 166, 185, 202, 218, 232, 244, 251, 255, 254, 248, 237, 224, 209, 195,
        183, 175, 168, 164, 160, 154, 147, 137, 124, 111, 99, 90, 84, 82, 84, 87, 92,
        97, 100, 101, 100, 99, 99, 99, 101, 105, 110, 116, 121, 125, 128, 128, 127,
        123, 120, 116, 113, 110, 109, 108, 106, 104, 101, 97, 92, 87, 81, 77, 73, 71,
        71, 72, 73, 75, 77, 79, 81, 83, 85, 88, 91, 95, 100, 105, 111, 116, 121, 125,
        129, 132, 135, 137, 140, 142, 144, 146, 147, 148, 148, 148, 147, 145, 143,
        141, 138, 136, 134, 132, 130, 127, 125, 122, 119, 117, 114, 112, 111, 109,
        108, 108, 107, 107, 107, 107, 107, 108, 109, 110, 111, 113, 114, 116, 118,
        119, 121, 123, 125, 127, 129, 132, 135, 139, 143, 147, 152, 157, 162, 167,
        173, 178, 184, 189, 195, 201, 207, 213, 218, 223, 228, 231, 234, 236, 236,
        236, 235, 233, 229, 225, 221, 215, 210, 203, 196, 188, 179, 171, 162, 153,
        145, 137, 129, 122, 115, 109, 104, 99, 94, 91, 88, 86, 84, 83, 83, 83, 84, 86,
        88, 90, 93, 96, 100, 104, 107, 111, 115, 119, 123, 126, 130, 133, 136, 139,
        142, 144, 146, 147, 148, 148, 148, 148, 147, 146, 145, 143, 142, 140, 139,
        137, 135, 134, 132, 131, 130, 129, 129, 129, 130, 130, 131, 133, 134, 136,
        138, 140, 142, 144, 146, 148, 150, 151, 152, 153, 154, 155, 155, 154, 153,
        152, 151, 149, 147, 144, 141, 138, 135, 132, 129, 125, 122, 119, 115, 112,
        109, 107, 105, 103, 101, 100, 99, 98, 97, 97, 97, 98, 98, 99, 100, 101, 103,
        104, 105, 106, 107, 108, 109, 110, 111, 112, 112, 112, 112, 112, 112, 111,
        110, 110, 108, 107, 106, 104, 103, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92,
        92, 92, 91, 91, 91, 92, 92, 93, 93, 94, 95, 97, 98, 99, 101, 102, 103, 105,
        106, 107, 109, 110, 111, 113, 114, 115, 116, 117, 118, 119, 120, 121, 121,
        122, 122, 122, 122, 122, 122, 122, 122, 122, 121, 121, 121, 120, 120, 120,
        120, 120, 119, 119, 119, 119, 119, 119, 119, 120, 120, 119, 119, 119, 118,
        118, 117, 117, 116, 116, 115, 114, 113, 112, 111, 110, 109, 109, 108, 107,
        107, 106, 105, 105, 105, 104, 104, 104, 105, 105, 106, 106, 107, 107, 108,
        108, 109, 109, 110, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 119,
        120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 132, 133,
        134, 135, 135, 136, 136, 137, 137, 137, 138, 138, 138, 139, 139, 139, 139,
        139, 139, 138, 138, 138, 138, 138, 137, 137, 137, 137, 137, 136, 136, 136,
        136, 135, 135, 135, 134, 134, 134, 134, 133, 133, 133, 133, 132, 132, 132,
        132, 132, 131, 131, 131, 131, 131, 131, 132, 132, 132, 132, 132, 133, 133,
        132, 132, 132, 132, 132, 131, 132, 132, 132, 132, 132, 132, 132, 132, 132,
        131, 131, 131, 130, 130, 130, 130, 130, 129, 129, 129, 129, 129, 129, 130,
        130, 130, 130, 131, 131, 131, 132, 132, 132, 133, 133, 133, 134, 134, 134,
        135, 135, 135, 135, 135, 136, 136, 136, 136, 136, 137, 137, 137, 137, 137,
        137, 137, 137, 137, 137, 136, 136, 136, 136, 136, 135, 135, 135, 135, 135,
        135, 135, 135, 134, 134, 133, 133, 133, 132, 132, 132, 131, 131, 130, 130,
        129, 129, 128, 128, 127, 127, 127, 126, 126, 125, 125, 124, 124, 124, 123,
        123, 123, 122, 122, 122, 122, 122, 122, 121, 121, 121, 121, 121, 121, 122,
        122, 122, 122, 122, 123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124,
        124, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 125, 125,
        125, 125, 125, 125, 125, 125, 126, 126, 126, 125, 125, 125, 125, 125, 125,
        125, 126, 126, 126, 125, 125, 125, 125, 125, 125, 125, 126, 126, 125, 125,
        125, 125, 125, 124, 124, 124, 124, 124, 124, 123, 123, 123, 123, 123, 123,
        123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 123, 123, 123, 123,
        123, 122, 122, 122, 122, 122, 122, 122, 121, 122, 122, 122, 122, 122, 123,
        123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 124, 124, 124, 125, 125,
        125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 127, 127, 127, 127,
        127, 127, 127, 127, 128, 128, 128, 128, 128, 129, 129, 129, 129, 129, 129,
        129, 130, 130, 130, 130, 130, 131, 131, 130, 130, 130, 130, 130, 129, 130,
        130, 130, 130, 130, 130, 130, 130, 130, 129, 129, 129, 129, 129, 128, 128,
        129, 129, 129, 129, 129, 129, 129, 129, 129, 129, 128, 128, 128, 128, 128,
        128, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 130, 130,
        130, 130, 130, 130, 130, 129, 129, 130, 130, 130, 130, 130, 131, 131, 131,
        130, 130, 130, 130, 130, 130, 130, 130, 130, 131, 131, 131, 131, 131, 132,
        132, 131, 131, 131, 131, 131, 130, 131, 131, 131, 131, 131, 132, 132, 132,
        132, 132, 131, 131, 131, 131, 131, 131, 131, 132, 132, 132, 131, 131, 131,
        131, 131, 130, 130, 130, 130, 130, 129, 129, 129, 129, 129, 129, 129, 129,
        129, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
        128, 128, 128, 129, 129, 129, 129, 129, 130, 130, 129, 129, 129, 129, 129,
        128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, 129, 129, 128, 128,
        128, 128, 128, 128, 128, 129, 129, 129, 129, 129, 130, 130, 129, 129, 129,
        129, 129, 129, 129, 129, 129, 130, 129, 129, 129, 129, 129, 128, 128, 129,
        129, 129, 129, 129, 129, 129, 129, 129, 129, 128, 128, 128, 128, 128, 128,
        128, 129, 129, 129, 129, 129, 129, 129, 128, 128, 128, 128, 128, 127, 127,
        128, 128, 128, 128, 128, 129, 128, 128, 128, 128, 128, 127, 127, 128, 128,
        128, 128, 128, 129, 129, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129,
        129, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, 129,
        130, 129, 129, 129, 129, 129, 128, 128, 128, 129, 129, 129, 129, 129, 130,
        130, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130,
        129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 129, 129,
        129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 129, 129, 129, 129,
        129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 129, 129, 129, 129, 129,
        129, 130, 130, 130, 130, 130, 130, 130, 130, 129, 129, 129, 129, 129, 129,
        129, 130, 130, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130,
        130, 130, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 131,
        131, 130, 130, 130, 130, 130, 129, 130, 130, 130, 130, 130, 131, 131, 130,
        130, 130, 130, 130, 129, 129, 130, 130, 130, 130, 130, 131, 131, 131, 131,
        131, 131, 130, 130, 130, 130, 130, 129, 129, 130, 130, 130, 130, 130, 131,
        131, 131, 131, 131, 131, 131, 131, 131, 130, 130, 130, 130, 130, 130, 130,
        130, 131, 130, 130, 130, 130, 130, 129, 129, 130, 130, 130, 130, 130, 130,
        130, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 131, 131,
        130, 130, 130, 130, 130, 129, 129, 129, 129, 129, 129, 129, 129, 129, 130,
        130, 130, 130, 130, 130, 130, 129, 130, 130, 130, 130, 130, 131, 131, 130,
        130, 130, 130, 130, 129, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130,
        130, 130, 130, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130,
        130, 129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 129,
        129, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 129, 129, 129,
        129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 130, 129, 129, 129, 129,
        129, 128, 128, 129, 129, 129, 129, 129, 130, 129, 129, 129, 129, 129, 128,
        128, 129, 129, 129, 129, 129, 130, 130, 130, 130, 130, 130, 130, 130, 130,
        129, 129, 129, 129, 129, 128, 129, 129, 129, 129, 129, 130, 130, 129, 129,
        129, 129, 129, 129, 129, 129, 129, 130, 129, 129, 129, 129, 129, 129, 129,
        129, 129, 129, 130, 130, 129, 129, 129, 129, 129, 128, 128, 129, 129, 129,
        129, 129, 130, 130, 129, 129, 129, 129, 129, 128, 129, 129, 129, 129, 129,
        129, 129, 129, 128, 128, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129,
        129, 129, 129, 128, 128, 128, 128, 128, 128, 128, 129, 129, 129, 129, 129,
        129, 128, 128, 128, 128, 128, 128, 127, 128, 128, 128, 128, 128, 129, 129,
        128, 128, 128, 128, 128, 127, 127, 128, 128, 128, 128, 128, 129, 128, 128,
        128, 128, 128, 127, 127, 128, 128, 128, 128, 128, 129, 128, 128, 128, 128,
        128, 127, 127, 127, 127, 127, 127, 127, 128, 128, 128, 128, 127, 127, 127,
        127, 127, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 126, 125, 125,
        126, 126, 126, 126, 126, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
        127, 126, 126, 126, 126, 126, 125, 126, 126, 126, 126, 126, 127, 127, 127,
        126, 126, 126, 126, 126, 125, 125, 126, 126, 126, 126, 126, 126, 126, 126,
        126, 125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 126, 126, 126, 125,
        125, 125, 125, 125, 125, 125, 125, 126, 126, 126, 125, 125, 125, 125, 125,
        124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 124, 125, 125, 125, 125,
        125, 125, 125, 124, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 125,
        125, 124, 124, 124, 124, 124, 124, 124, 124, 125, 125, 125, 125, 125, 125,
        125, 124, 124, 124, 124, 124, 123, 123, 124, 124, 124, 124, 124, 124, 124,
        123, 123, 123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 123, 123,
        123, 123, 123, 123, 123, 124, 124, 124, 124, 124, 124, 123, 123, 123, 123,
        123, 123, 123, 123, 124, 124, 124, 123, 123, 123, 123, 123, 122, 123, 123,
        123, 123, 123, 124, 124, 123, 123, 123, 123, 123, 122, 122, 122, 122, 122,
        122, 122, 122, 123, 123, 123, 123, 123, 124, 124, 123, 123, 123, 123, 123,
        122, 123, 123, 123, 123, 123, 123, 123, 123, 123, 122, 122, 122, 122, 122,
        121, 121, 122, 122, 122, 122, 122, 123, 123, 123, 123, 122, 122, 122, 122,
        122, 121, 121, 122, 122, 122, 122, 122, 122, 122, 122, 121, 121, 121, 121,
        121, 121, 121, 122, 122, 122, 122, 122, 122, 122, 121, 121, 121, 121, 121,
        121, 122, 122, 122, 122, 122, 122, 122, 121, 121, 121, 121, 121, 121, 122,
        122, 122, 122, 122, 122, 122, 121, 121, 121, 121, 121, 121, 121, 122, 122,
        122, 122, 122, 122, 121, 121, 121, 121, 121, 120, 120, 121, 121, 121, 121,
        121, 121, 121, 120, 120, 120, 120, 120, 120, 120, 121, 121, 121, 121, 121,
        121, 120, 120, 120, 120, 120, 119, 120, 120, 120, 120, 120, 121, 121, 121,
        121, 121, 120, 120, 120, 120, 119, 119, 119, 119, 119, 119, 120, 120, 120,
        120, 120, 121, 121, 120, 120, 120, 120, 120, 119, 119, 119, 119, 119, 119,
        119, 119, 119, 119, 118, 118, 119, 119, 119, 119, 119, 120, 119, 119, 119,
        119, 119, 119, 119, 119, 119, 119, 119, 119, 118, 118, 118, 118, 118, 117,
        117, 118, 118, 118, 118, 118, 119, 119, 119, 119, 119, 118, 118, 118, 118,
        118, 118, 118, 118, 118, 119, 119, 119, 118, 118, 118, 118, 118, 117, 117,
        117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 117, 118, 118, 118, 118,
        118, 118, 118, 117, 117, 117, 117, 117, 117, 118, 118, 118, 118, 118, 118,
        118, 117, 117, 117, 117, 117, 117, 117, 117, 118, 118, 117, 117, 117, 117,
        117, 116, 116, 117, 117, 117, 117, 117, 118, 117, 117, 117, 117, 117, 116,
        116, 117, 117, 117, 117, 117, 118, 117, 117, 117, 117, 117, 116, 116, 117,
        117, 117, 117, 117, 118, 118, 117, 117, 117, 117, 117, 116, 116, 116, 116,
        116, 115, 115, 116, 116, 116, 116, 116, 117, 116, 116, 116, 116, 116, 115,
        115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 116, 116, 116,
        116, 116, 116, 116, 115, 115, 115, 115, 115, 115, 116, 116, 116, 116, 116,
        116, 116, 115, 115, 115, 115, 115, 114, 114, 115, 115, 115, 115, 115, 116,
        115, 115, 115, 115, 115, 114, 114, 115, 115, 115, 115, 115, 116, 116, 115,
        115, 115, 115, 115, 114, 115, 115, 115, 115, 115, 115, 115, 115, 115, 115,
        114, 114, 114, 114, 114, 114, 114, 114, 115, 114, 114, 114, 114, 114, 114,
        115, 115, 115, 115, 115, 115, 115, 114, 114, 114, 114, 114, 113, 113, 114,
        114, 114, 114, 114, 114, 114, 114, 114, 114, 113, 113, 113, 114, 114, 114,
        114, 114, 114, 113, 113, 113, 113, 113, 113, 113, 114, 114, 114, 114, 114,
        114, 114, 113, 113, 113, 113, 113, 113, 113, 114, 114, 114, 113, 113, 113,
        113, 113, 112, 112, 113, 113, 113, 113, 113, 114, 114, 113, 113, 113, 113,
        113, 112, 112, 112, 112, 112, 112, 112, 113, 113, 113, 113, 113, 113, 113,
        113, 113, 112, 112, 112, 112, 113, 113, 113, 113, 113, 113, 113, 112, 112,
        112, 112, 112, 112, 112, 112, 112, 113, 113, 113, 112, 112, 112, 112, 112,
        112, 112, 113, 113, 112, 112, 112, 112, 112, 111, 111, 112, 112, 112, 112,
        112, 113, 112, 112, 112, 112, 112, 111, 111, 112, 112, 112, 112, 112, 113,
        112, 112, 112, 112, 112, 111, 111, 112, 112, 112, 112, 112, 113, 112, 112,
        112, 112, 112, 111, 111, 111, 111, 111, 110, 110, 111, 111, 111, 111, 111,
        112, 111, 111, 111, 111, 111, 110, 110, 111, 111, 111, 111, 111, 111, 111,
        111, 111, 110, 110, 110, 110, 110, 110, 110, 111, 111, 111, 111, 111, 112,
        112, 111, 111, 111, 111, 111, 111, 111, 111, 112, 112, 111, 111, 111, 111,
        111, 110, 110, 110, 110, 110, 109, 110, 110, 110, 110, 110, 111, 111, 111,
        110, 110, 110, 110, 110, 109, 109, 110, 110, 110, 110, 110, 111, 111, 110,
        110, 110, 110, 110, 109, 110, 110, 110, 110, 110, 110, 110, 110, 110, 109,
        109, 110, 110, 110, 110, 110, 110, 109, 109, 109, 109, 109, 108, 108, 109,
        109, 109, 109, 109, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
        110, 110, 109, 109, 109, 109, 109, 109, 109, 110, 110, 110, 110, 110, 110,
        109, 109, 109, 109, 109, 108, 109, 109, 109, 109, 109, 110, 110, 110, 110,
        110, 109, 109, 109, 109, 109, 109, 109, 109, 110, 109, 109, 109, 109, 109,
        108, 108, 109, 109, 109, 109, 109, 110, 110, 109, 109, 109, 109, 109, 108,
        109, 109, 109, 109, 109, 110, 110, 109, 109, 109, 109, 109, 108, 108, 109,
        109, 109, 109, 109, 110, 109, 109, 109, 109, 109, 108, 108, 109, 109, 109,
        109, 109, 110, 109, 109, 109, 109, 109, 108, 108, 109, 109, 109, 109, 109,
        110, 110, 110, 110, 110, 110, 110, 109, 109, 109, 109, 109, 109, 110, 110,
        110, 110, 110, 109, 109, 109, 109, 109, 108, 108, 108, 108, 108, 108, 108,
        109, 109, 108, 108, 108, 108, 108, 108, 108, 109, 109, 109, 109, 108, 108,
        108, 108, 108, 107, 107, 108, 108, 108, 108, 108, 108, 107, 107, 107, 107,
        107, 107, 107, 108, 108, 108, 108, 108, 108, 107, 107, 107, 107, 107, 107,
        107, 108, 108, 108, 108, 108, 108, 108, 107, 107, 107, 107, 107, 107, 108,
        108, 108, 108, 108, 108, 108, 107, 107, 107, 107, 107, 107, 107, 108, 108,
        108, 108, 108, 108, 107, 107, 107, 107, 107, 107, 107, 108, 108, 108, 108,
        108, 108, 108, 107, 107, 107, 107, 107, 107, 107, 108, 108, 108, 108, 108,
        108, 108, 107, 107, 107, 107, 107, 106, 106, 107, 107, 107, 107, 107, 107,
        107, 107, 107, 106, 106, 106, 107, 107, 107, 107, 107, 108, 108, 107, 107,
        107, 107, 107, 106, 107, 107, 107, 107, 107, 108, 108, 107, 107, 107, 107,
        107, 106, 107, 107, 107, 107, 107, 108, 108, 107, 107, 107, 107, 107, 106,
        106, 107, 107, 107, 107, 107, 108, 107, 107, 107, 107, 107, 106, 106, 107,
        107, 107, 107, 107, 108, 107, 107, 107, 107, 107, 106, 106, 106, 106, 106,
        106, 106, 107, 107, 107, 107, 107, 107, 106, 106, 106, 106, 106, 106, 107,
        107, 107, 107, 107, 107, 107, 106, 106, 106, 106, 106, 106, 106, 107, 107,
        107, 107, 107, 107, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
        107, 107, 106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107, 107, 107,
        107, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 105, 106, 106,
        106, 106, 106, 106, 107, 107, 107, 107, 107, 107, 106, 106, 106, 106, 106,
        106, 106, 106, 106, 107, 107, 106, 106, 106, 106, 106, 105, 105, 106, 106,
        106, 106, 106, 107, 107, 107, 106, 106, 106, 106, 106, 106, 106, 106, 106,
        107, 107, 106, 106, 106, 106, 106, 106, 106, 106, 106, 107, 106, 106, 106,
        106, 106, 105, 105, 106, 106, 106, 106, 106, 107, 107, 107, 107, 106, 106,
        106, 106, 106, 106, 106, 106, 106, 107, 107, 107, 106, 106, 106, 106, 106,
        105, 105, 106, 106, 106, 106, 106, 107, 106, 106, 106, 106, 106, 105, 105,
        105, 105, 105, 105, 106, 106, 106, 106, 106, 107, 107, 106, 106, 106, 106,
        106, 105, 106, 106, 106, 106, 106, 107, 107, 106, 106, 106, 106, 106, 105,
        106, 106, 106, 106, 106, 107, 107, 106, 106, 106, 106, 106, 105, 105, 106,
        106, 106, 106, 106, 107, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105,
        105, 106, 106, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 106,
        106, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 105, 106, 106,
        106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106,
        106, 106, 106, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 107,
        107, 106, 106, 106, 106, 106, 105, 106, 106, 106, 106, 106, 107, 107, 106,
        106, 106, 106, 106, 105, 105, 106, 106, 106, 106, 106, 106, 106, 106, 106,
        106, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 105,
        105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106, 105, 105,
        105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106, 105, 105, 105, 105,
        105, 105, 105, 106, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105,
        105, 106, 106, 106, 106, 106, 107, 107, 106, 106, 106, 106, 106, 105, 106,
        106, 106, 106, 106, 107, 107, 107, 107, 107, 106, 106, 106, 106, 106, 105,
        105, 106, 106, 106, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105,
        105, 106, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 105, 106,
        106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106,
        106, 106, 107, 107, 106, 106, 106, 106, 106, 105, 106, 106, 106, 106, 106,
        107, 107, 106, 106, 106, 106, 106, 105, 105, 106, 106, 106, 106, 106, 106,
        106, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106, 105,
        105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 105, 105, 105, 105, 105,
        104, 105, 105, 105, 105, 105, 106, 106, 105, 105, 105, 105, 105, 105, 105,
        106, 106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 105, 106, 106,
        106, 106, 106, 106, 106, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106,
        106, 106, 106, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106,
        106, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106,
        105, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106, 105, 105,
        105, 105, 105, 104, 104, 105, 105, 105, 105, 105, 106, 105, 105, 105, 105,
        105, 104, 104, 105, 105, 105, 105, 105, 106, 106, 105, 105, 105, 105, 105,
        104, 105, 105, 105, 105, 105, 106, 106, 105, 105, 105, 105, 105, 104, 104,
        105, 105, 105, 105, 105, 106, 106, 106, 106, 106, 106, 106, 105, 105, 105,
        105, 105, 105, 105, 106, 106, 106, 105, 105, 105, 105, 105, 104, 104, 104,
        104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105,
        105, 105, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 105,
        105, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105,
        104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105, 105, 105, 105,
        104, 105, 105, 105, 105, 105, 106, 106, 105, 105, 105, 105, 105, 104, 104,
        105, 105, 105, 105, 105, 106, 106, 105, 105, 105, 105, 105, 104, 104, 104,
        104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105,
        105, 106, 106, 105, 105, 105, 105, 105, 104, 105, 105, 105, 105, 105, 106,
        106, 105, 105, 105, 105, 105, 104, 104, 104, 104, 104, 104, 105, 105, 105,
        105, 105, 105, 105, 104, 104, 104, 104, 104, 104, 104, 105, 105, 105, 105,
        105, 105, 105, 105, 105, 104, 104, 104, 104, 104, 105, 105, 105, 105, 105,
        106, 106, 105, 105, 105, 105, 105, 105, 105, 105, 105, 106, 106, 106, 106,
        106, 106, 105, 105, 105, 105, 105, 104, 104, 105, 105, 105, 105, 105, 106,
        105, 105, 105, 105, 105, 104, 104, 104, 104, 104, 104, 104, 103, 103, 104,
        104, 104, 104, 104, 104, 104, 104, 104, 103, 103, 103, 103, 103, 103, 103,
        104, 104, 104, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104, 104,
        104, 103, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 103,
        103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 103, 103, 103,
        103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 104, 103, 103, 103, 103,
        103, 103, 104, 104, 104, 104, 104, 105, 105, 104, 104, 104, 104, 104, 103,
        104, 104, 104, 104, 104, 105, 105, 104, 104, 104, 104, 104, 103, 103, 104,
        104, 104, 104, 104, 105, 104, 104, 104, 104, 104, 103, 103, 104, 104, 104,
        104, 104, 105, 104, 104, 104, 104, 104, 103, 103, 104, 104, 104, 104, 104,
        105, 105, 104, 104, 104, 104, 104, 103, 103, 103, 103, 103, 103, 103, 104,
        104, 104, 104, 104, 104, 103, 103, 103, 103, 103, 103, 103, 104, 104, 104,
        104, 104, 104, 104, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104,
        104, 104, 103, 103, 103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 105,
        105, 105, 105, 105, 105, 105, 104, 105, 105, 105, 105, 103, 99, 91, 78, 61,
        43, 26, 12, 3, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
        1, 2, 2, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
        1, 2, 2, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    size_t offset   = 0;
    char  *portname = "/dev/ttymxc1"; // 串口设备名称
    int    baudrate = B115200;        // 波特率
    // serial_fd       = init_serial_port(portname, baudrate);
    // pthread_t read_thread;
    // pthread_create(&read_thread, NULL, read_serial, NULL);
    // printf("\ndatalen = %d\n", strlen(data));
    for (uint8_t frame_id = 0; offset < sizeof(data); frame_id++) {
        size_t  data_size = FRAME_SIZE;
        uint8_t frame[FRAME_SIZE + FRAME_HEADER_SIZE + FRAME_TAILER_SIZE];
        size_t  frame_size = pack_frame(data + offset, data_size, frame, frame_id);
        send_frame(frame, frame_size);

        // while (write(serial_fd, frame, frame_size) < 0) {
        //     printf("Resending frame %d\n", frame_id);
        //     usleep(100000);
        // }
        // printf("\nT:NO.%d frame sended\n", frame_id);
        // sleep(1);
        // usleep(500000);
        // read_from_serial_port(serial_fd);
        offset += data_size;
    }
    // pthread_join(read_thread, NULL);
    close(serial_fd);
    return 0;
}
